/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mbean.admin.solution;

import baiduMapPojo.APIParameter;

import java.io.IOException;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import model.entity.Depot;
import model.entity.Problem;
import model.entity.Task;
import okhttp3.OkHttpClient;
import service.InterfaceServiceLocal;

/**
 *
 * @author dingnangen
 */
@Named(value = "linesMB")
@SessionScoped
public class LinesMB implements Serializable {

    @EJB(beanName = "ProblemManager")
    private InterfaceServiceLocal<Problem> problemManager;
    Problem p;
    OkHttpClient client = new OkHttpClient();
    String directionJson;

    String linesArray = "";

    String URL = APIParameter.directionURL;

    /**
     * Creates a new instance of LinesMB
     */
    public LinesMB() throws IOException {

    }

    @PostConstruct
    public void init() {
        p = problemManager.find(10);

    }

    public String getLinesArray() {
        System.out.println("p:"+p);
        linesArray = "";
//        if (!"".equals(linesArray)) {
//            return linesArray;
//        }
        if (p.getSolutionList().isEmpty()) {
            return linesArray;
        };
        Depot depot = p.getIddepot();
        System.out.println(p.getSolutionList().size());
        int SolutionListsize = p.getSolutionList().size();
        List< Task> tl = p.getSolutionList().get(SolutionListsize - 1).getTaskList();
        for (Task task : tl) {
            String strokeColor = getRandColor();
            linesArray += "new BMap.Polyline(  " + task.getDirectionstring();
            linesArray += ", {strokeColor:'" + strokeColor + "', strokeWeight:5, strokeOpacity:0.8}),";
        }
//        for (int t = 0; t < p.getTaskCollection().size(); t++) {
//            entities.Task task = tl.get(t);
//
//            String strokeColor = getRandColor();
//            linesArray += "new BMap.Polyline(  " + task.getDirectionstring();
//            linesArray += ", {strokeColor:'" + strokeColor + "', strokeWeight:3, strokeOpacity:0.7}),";
//
//        }

        linesArray = linesArray.substring(0, linesArray.length() - 1);

        return linesArray;
    }

    public String getRandColor() {
        String s = "#";
        for (int i = 0; i < 3; i++) {
            s += Integer.toHexString((int) (Math.random() * (15 + 1)));//1-15,避免白色
        }
        System.out.println(s);
        return s;

    }

    public void setLinesArray(String linesArray) {
        this.linesArray = linesArray;
    }

}
